<?php

class Upload extends CI_Controller {
	
	function index(){
		//block index access
		show_404(base_url());
	}
	
	function ideaImage(){
		/*
		 * Upload image of an idea and create thumbnails
		 */
		$this->load->model('process_image_model');
		$id = $this->input->post('idea_id');
		
		//initialize config
		$config['upload_path'] = './images/upload/ideas/';
		$config['allowed_types'] = 'gif|jpg|png';
		$config['max_size'] = '4096'; //4mb

		$this->load->library('upload', $config);

		if ( ! $this->upload->do_upload('image'))
		{
			$error = array('error' => $this->upload->display_errors());
			//echo error message
			foreach($error as $err){
				echo $err;
			}
			echo "error uploading image";
		}
		else
		{
			//process uploaded image
			$imageName = $this->process_image_model->process_image(1, $id);
			
			//track activity
			$this->load->model('feed_model');
			$this->feed_model->trackIdeaActivity($id, " uploaded a new picture.");
			
			//echo new image
			echo img(array(
						'src' => './images/upload/ideas/'.$imageName,
						'alt' => $imageName,
						'width' => '900px',
						'title' => $imageName
					));;
		}
	}
	
	function profile(){
		/*
		 * Load change profile picture page
		 */
		include("authenticate.php");
		
		$this->load->view('view_change_profile_pict');
	}
	
	function profilePict(){
		/*
		 * Handle image to be used as a profile picture
		 */
		$this->load->model('process_image_model');
		$id = $this->input->post('user_id');
		
		//initialize config
		$config['upload_path'] = './images/upload/user/';
		$config['allowed_types'] = 'gif|jpg|png';
		$config['max_size'] = '2048'; //2mb
		
		$this->load->library('upload', $config);
		
		if ( ! $this->upload->do_upload('image'))
		{
			$error = array('error' => $this->upload->display_errors());
			//echo error message
			echo "error uploading image";
		}
		else
		{
			//process image
			$imageName = $this->process_image_model->process_image(2, $id);
			
			//track activity
				$this->load->model('feed_model');
				$this->feed_model->trackUserActivity($username, " has a new profile picture.");
			
			//echo new image
			echo img(array(
						'src' => './images/upload/user/'.$imageName,
						'alt' => $imageName,
						'width' => '350px',
						'title' => $imageName
					));
		}
	}
}

?>